package arithmetic.baseAlgorithm.sort;

/**
 * 插入排序
 * 将候选的值插入已经排序好的队列中
 * 时间复杂度:
 * n^2
 *
 * @author zhangyanqi
 * @since 1.0 2017/10/18
 */
public class InsertionSort {

    public static void sort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int a = nums[i];
            for (int j = 0; j < i; j++) {
                if (nums[j] > a) {
                    int temp = nums[j];
                    nums[j] = a;
                    for (int k = j + 1; k <= i; k++) {
                        int temp2 = nums[k];
                        nums[k] = temp;
                        temp = temp2;
                    }
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[] ints = {3, 2, 1, 5, 7, 6};
        sort(ints);
        System.out.println(ints[0] + " " + ints[1] + " " + ints[2] + " " + ints[3] + " " + ints[4] + " " + ints[5]);
    }
}
